//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.medaxis.hpams.sso.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import org.apache.commons.lang3.time.DateFormatUtils;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static final TimeZone GMT8_TIMEZONE = TimeZone.getTimeZone("GMT+8");
    public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM", "yyyyMM"};

    public DateUtils() {
    }

    public static Date getGMT8Time() {
        return Calendar.getInstance(GMT8_TIMEZONE).getTime();
    }

    public static Date getNowDate() {
        return new Date();
    }

    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(String format, Date date) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static final Date dateTime(String format, String ts) {
        try {
            return (new SimpleDateFormat(format)).parse(ts);
        } catch (ParseException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        } else {
            try {
                return parseDate(str.toString(), parsePatterns);
            } catch (ParseException var2) {
                return null;
            }
        }
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long diff = endDate.getTime() - nowDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }

    public static Date localDateToDate(LocalDate localDate) {
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    public static LocalDate getFirstDayOfMonth(LocalDate localDate) {
        return localDate.with(TemporalAdjusters.firstDayOfMonth());
    }

    public static String getSeason(Date date) {
        String season = null;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(2);
        switch(month) {
        case 0:
        case 1:
        case 2:
            season = "第一季度";
            break;
        case 3:
        case 4:
        case 5:
            season = "第二季度";
            break;
        case 6:
        case 7:
        case 8:
            season = "第三季度";
            break;
        case 9:
        case 10:
        case 11:
            season = "第四季度";
        }

        return season;
    }

    public static String getMonth(Date date) {
        LocalDate localDate = dateToLocalDate(date);
        return String.valueOf(localDate.getMonth().getValue());
    }

    public static Date getEndMonth(List<Date> startEndMonth, String timeItem, String type) {
        if (timeItem == null) {
            return (Date)startEndMonth.get(1);
        } else {
            Date end = (Date)startEndMonth.get(1);
            if ("总计".equals(timeItem)) {
                return end;
            } else {
                String year = timeItem.substring(0, 4);
                Date date;
                if ("按月统计".equals(type)) {
                    String month;
                    if (timeItem.length() == 7) {
                        month = timeItem.substring(5, 6);
                    } else {
                        month = timeItem.substring(5, 7);
                    }

                    date = parseDate(year + month);
                    return date;
                } else {
                    if ("按季度统计".equals(type)) {
                        if (timeItem.substring(4).equals("第一季度")) {
                            date = parseDate(year + "03");
                        } else if (timeItem.substring(4).equals("第二季度")) {
                            date = parseDate(year + "06");
                        } else if (timeItem.substring(4).equals("第三季度")) {
                            date = parseDate(year + "09");
                        } else {
                            date = parseDate(year + "12");
                        }
                    } else if ("按半年统计".equals(type)) {
                        if (timeItem.substring(4).equals("上半年")) {
                            date = parseDate(year + "06");
                        } else {
                            date = parseDate(year + "12");
                        }
                    } else {
                        date = parseDate(year + "12");
                    }

                    return end.compareTo(date) < 0 ? end : date;
                }
            }
        }
    }

    public static String getHalfYear(Date date) {
        String halfYear = null;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(2);
        switch(month) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            halfYear = "上半年";
            break;
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
            halfYear = "下半年";
        }

        return halfYear;
    }

    public static int getBetweenMonth(List<Date> startEndMonth) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime((Date)startEndMonth.get(0));
        end.setTime((Date)startEndMonth.get(1));
        int result = end.get(2) - start.get(2);
        int month = (end.get(1) - start.get(1)) * 12;
        return result + month;
    }

    public static boolean before(Date time, Date time1) {
        return time.compareTo(time1) < 0;
    }

    public static Integer getMonthSize(String type) {
        if ("按月统计".equals(type)) {
            return 1;
        } else if ("按季度统计".equals(type)) {
            return 3;
        } else {
            return "按半年统计".equals(type) ? 6 : 12;
        }
    }

    public static Date getStartMonth(Date startDate, String timeItem, String type) {
        if ("总计".equals(timeItem)) {
            return startDate;
        } else if (timeItem == null) {
            return startDate;
        } else {
            String year = timeItem.substring(0, 4);
            Date date;
            if ("按月统计".equals(type)) {
                String month;
                if (timeItem.length() == 7) {
                    month = timeItem.substring(5, 6);
                } else {
                    month = timeItem.substring(5, 7);
                }

                date = parseDate(year + month);
                return date;
            } else {
                if ("按季度统计".equals(type)) {
                    if (timeItem.substring(4).equals("第一季度")) {
                        date = parseDate(year + "01");
                    } else if (timeItem.substring(4).equals("第二季度")) {
                        date = parseDate(year + "04");
                    } else if (timeItem.substring(4).equals("第三季度")) {
                        date = parseDate(year + "07");
                    } else {
                        date = parseDate(year + "10");
                    }
                } else if ("按半年统计".equals(type)) {
                    if (timeItem.substring(4).equals("上半年")) {
                        date = parseDate(year + "01");
                    } else {
                        date = parseDate(year + "06");
                    }
                } else {
                    date = parseDate(year + "01");
                }

                return startDate.compareTo(date) > 0 ? startDate : date;
            }
        }
    }
}
